package com.szj.controller;

import com.szj.entity.Admin;
import com.szj.entity.LoginForm;
import com.szj.entity.Student;
import com.szj.entity.Teacher;
import com.szj.service.AdminService;
import com.szj.service.StudentService;
import com.szj.service.TeacherService;
import com.szj.util.CreateVerifiCodeImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/system")
public class SystemController {

    private Map<String,Object> result=new HashMap<String,Object>();

    @Autowired
    private AdminService adminService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 用户退出
     * @param request
     * @param response
     */
    @GetMapping("/loginOut")
    public void loginOut(HttpServletRequest request,HttpServletResponse response){
        //清空域中用户的信息
        request.getSession().removeAttribute("userInfo");
        request.getSession().removeAttribute("userType");
        //注销 后重定向到登录页面
        try {
            response.sendRedirect("../index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 跳转到登录页面
     * @return
     */
    @GetMapping ("/goLogin")
    public String goLogin(){
        return "system/login";
    }

    /**
     * 生成验证码进行返回到前端
     * @param request
     * @param response
     */
    @GetMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response){
        System.out.println("123");
        System.out.println(request);
        System.out.println(response);

        //工具类生成验证码图片
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
        //获取验证码随机生成的4位字符
        String verifiCode = String.valueOf(CreateVerifiCodeImage.getVerifiCode());
        //输出到前端进行展示
        try {
            ImageIO.write(verifiCodeImage,"JPEG",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //存储验证码到session域中
        request.getSession().setAttribute("verifiCode",verifiCode);
    }

    /**
     * 用户登录
     * @param loginForm
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ResponseBody()
    public Map<String, Object> login(LoginForm loginForm, HttpServletRequest request) {
        //校验验证码信息  获取生成验证码信息
        String verifiCode = (String) request.getSession().getAttribute("verifiCode");
        System.out.println(verifiCode);
        if ("".equals(verifiCode)) {
            result.put("success", false);
            result.put("msg", "长时间未操作，请刷新页面重试！");
            return result;
        } else if (!(verifiCode.equalsIgnoreCase(loginForm.getVerifiCode()))) {
            result.put("success", false);
            result.put("msg", "验证码输入错误！");
            return result;
        }
        //清除session域中验证码
        request.getSession().removeAttribute("verifiCode");
        //用户登录请求 1管理员 2 学生 3 老师
        switch (loginForm.getUserType()) {
            //1管理员
            case "1":
                try {
                    Admin admin = adminService.login(loginForm);
                    if (admin!=null){
                        //把信息存储到域中
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",admin);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","服务器发生异常！");
                    return result;
                }
                break;
            //2 学生
            case "2":
                try {
                    Student student = studentService.login(loginForm);
                    if (student!=null){
                        //把信息存储到域中
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",student);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","服务器发生异常！");
                    return result;
                }

                break;
            //3 老师
            case "3":
                try {
                    Teacher teacher = teacherService.login(loginForm);
                    if (teacher!=null){
                        //把信息存储到域中
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo",teacher);
                        session.setAttribute("userType",loginForm.getUserType());
                        result.put("success",true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success",false);
                    result.put("msg","服务器发生异常！");
                    return result;
                }
                break;
        }
        //登录失败
        result.put("success",false);
        result.put("msg","登陆失败");
        return result;
    }

    //跳转到主页面
    @GetMapping("/goSystemMainView")
    public String goSystemMainView(){
        return "system/main";
    }


}
